package tgc.rj.zz.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import tgc.rj.zz.custom.DataGridUtils;
import tgc.rj.zz.entity.Accredit;
import tgc.rj.zz.entity.Journal;
import tgc.rj.zz.entity.SysUser;
import tgc.rj.zz.form.AccreditForm;
import tgc.rj.zz.form.AccreditForm2;
import tgc.rj.zz.security.UserUtils;
import tgc.rj.zz.service.AccreditService;
import tgc.rj.zz.service.JournalService;

@Controller
@RequestMapping(value="accredit")
public class AccreditController {
	@Autowired
	private AccreditService accreditDAO;
	@Autowired
	private UserUtils userUtils;
	@Autowired
	private JournalService service;
	
	@RequestMapping(value="all")
	public String all() {
		return "system/sqgl";
	}

	@RequestMapping(value="zh_all")
	public String zh_all() {
		return "admin/zh_all";
	}
	
	@RequestMapping(value="/list")
	@ResponseBody
	public Object list(AccreditForm form) {
		Pageable pageable = form.buildPageable();
		Page<Accredit> page = null;
		if(form.getId() == null) {
			page = accreditDAO.findAll(pageable);			
		}else {
			page = accreditDAO.findAll(form.buildSpes(), pageable);
		}
		HashMap<String , Object> result = DataGridUtils.getPageResult(page);
		return result;
	}
	
	@RequestMapping(value="/list1")
	@ResponseBody
	public Object list1(AccreditForm form) {
		SysUser user = userUtils.getUser();
		Pageable pageable = form.buildPageable();
		Page<Accredit> page = null;
		System.out.println(user.getId());
		if(form.getId() == null) {
			page = accreditDAO.findByLesseeLesseeAdminId(user.getId(), pageable);			
		}else {
			page = accreditDAO.findByLesseeLesseeAdminId(user.getId(), form.buildSpes(), pageable);
		}
		HashMap<String , Object> result = DataGridUtils.getPageResult(page);
		return result;
	}
	

	@RequestMapping(value="/save")
	@ResponseBody
	public Object save (AccreditForm2 form) {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		Accredit ac=new Accredit();
		try {
			ac.setBeginDate(sf.parse(form.getBeginDate()));
			ac.setOverDate(sf.parse(form.getOverDate()));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		BeanUtils.copyProperties(form, ac);
		accreditDAO.save(ac);
		
		SysUser user = userUtils.getUser();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		String date = df.format(new Date());
		Journal journal = new Journal();
		journal.setDate(date);
		journal.setUsername(user.getUsername());
		journal.setOperationName("修改授权");
		service.save(journal);
		return "修改授权成功";
	}
}
